<?php
namespace app\oa\model;

use app\admin\model\DepartMent;
use app\admin\model\Duty;
use think\Db;
use think\Validate;
use app\oa\logic\User as UserLogicModel;

class User extends Base
{

//    protected $name='user';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;
//    //部门
//    static protected $department = [
//        1 => ['department' => '部门1', 'level' => 1],
//        2 => ['department' => '部门2', 'level' => 1],
//        3 => ['department' => '部门3', 'level' => 1],
//        4 => ['department' => '部门4', 'level' => 1],
//        5 => ['department' => '部门5', 'level' => 1]
//    ];
//    //职务
//    static protected $duty = [
//        1 => ['duty' => '职务1', 'level' => 1],
//        2 => ['duty' => '职务2', 'level' => 2],
//        3 => ['duty' => '职务3', 'level' => 3],
//        4 => ['duty' => '职务4', 'level' => 4],
//    ];
    //部门
    static protected $department = [];
    //职务
    static protected $duty = [];
    public $departmentObj;  //部门信息

    public $department_level; //部门级别

    public $dutyObj;//职务信息

    public $duty_level;//职务级别

    public function getStatusAttr($value)
    {
        $arr = [0 => '正常', 2 => '禁用'];
        return is_numeric($value) ? $arr[$value] : '没有状态';
    }

    public function getDepartmentIdAttr($value)
    {
        return $this->name('department')->where(['id' => $value])->field('depart_name')->find()['depart_name'];

    }

    public function getDutyIdAttr($value)
    {
        return $this->name('duty')->where(['id' => $value])->field('duty_name')->find()['duty_name'];
    }

    //处理器处理密码
    public function setPasswordAttr($value)
    {
        return encode($value);
    }

    public function __construct($data = [])
    {
        parent::__construct($data);
        //构造对象的属性
//        $dutyData=Duty::getAllInfo([],'duty_name,duty_level');
//        foreach ($dutyData as $index => $dutyDatum) {
//            self::$duty[$index]['duty']=$dutyDatum->duty_name;
//            self::$duty[$index]['level']=$dutyDatum->duty_level;
//        }
//        $departData=DepartMent::getAllInfo([],'depart_name,depart_level');
//        foreach ($departData as $index => $departDatum) {
//            self::$department[$index]['department']=$departDatum->depart_name;
//            self::$department[$index]['level']=$departDatum->depart_level;
//        }
        $this->departmentObj = isset($data['department_info']) ? $data['department_info'] : null;
        $this->department_level = isset($data['department_info']['level']) ? $data['department_info']['level'] : 0;
        $this->dutyObj = isset($data['duty_info']) ? $data['duty_info'] : null;
        $this->duty_level = isset($data['duty_info']['level']) ? $data['duty_info']['level'] : 0;

    }

    //一对多关联  一个用户有多个 工作进度汇报

    public function reports()
    {

        return $this->hasMany('report', 'user_id','uid');
    }

    public function missions()
    {

        return $this->hasMany('mission', 'user_id');
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description:获取资源列表
     * @time: 2017年7月20日 12:00:28
     * @param string $field
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getAllUser($field = '*')
    {
        return $this->where(['status' => 0])->select();
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description:  重构对象
     * @time: 2017年7月11日 09:08:42
     * @param array $data
     * @return static
     */
    public static function refactorObj($data = [])
    {
        return new static($data);
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description:创建一个对象实例并写入
     * @time: 2017年7月7日 09:43:07
     * @param $data
     * @return mixed
     */
    public static function addUser($data)
    {
        $mobile = $data['mobile'];
        $rules = [
            ['mobile', "require|length:11", '手机号必须填写|手机号格式不正确'],
            ['password', 'require|alphaNum|min:6', '密码必须填写|密码只能字母和数字|密码最小长度为6位'],
            ['department_id', 'require', '部门必须填写'],
            ['duty_id', 'require', '职务必须填写'],
            ['upuid', 'require', '直属领导必须填写'],
            ['realname', 'require', '名字必须填写'],
//            ['verify_code', 'require|length:6', '验证码必须填写|验证为6位']
        ];
        $validateObj = new Validate($rules);
        if (!$validateObj->batch()->check($data)) {
            return $validateObj->getError();
        }
        if (UserLogicModel::isRegister($mobile)) {
            return self::returnMsg('用户已经存在');
        }
        $userObj = self::create($data);
        if (is_object($userObj)) {
            return (int)$userObj->uid;
        } else {
            return self::returnMsg('注册失败');
        }
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description:  获得登录用户的信息 by手机号
     * @time:2017-7-10 16:19:12
     * @param $mobile
     * @param bool $type false 返回对象信息数据    true  返回一个对象
     * @return array
     */
    public static function getUserInfoByMobile($mobile, $type = false)
    {
        static $result_data = [];
        if (isset($result_data[$mobile])) {
            $userObj = $result_data[$mobile];
        } else {
            $result_data[$mobile] = User::getByMobile($mobile);
            $userObj = $result_data[$mobile];
        }
//        dump($userObj);
        if ($type === false) {
            if (is_object($userObj)) {
                $data = $userObj->hidden(['create_time', 'update_time'])->getData();
                $dutyData = Duty::getAllInfo(['id' => $data['duty_id']], 'duty_name,duty_level');
                if (!is_object($dutyData[0])) {
                    return self::returnMsg('职务信息为空', -1);
                }
                $departData = DepartMent::getAllInfo(['id' => $data['department_id']], 'depart_name,depart_level');
//                $data['department_info'] = isset(self::$department[$data['department_id']]) ? self::$department[$data['department_id']] : '没有部门';
                $data['department_info'] = ['department' => $departData[0]->depart_name, 'level' => $departData[0]->depart_level];
//                $data['duty_info'] = isset(self::$duty[$data['duty_id']]) ? self::$duty[$data['duty_id']] : '没有职务';
                $data['duty_info'] = ['duty' => $dutyData[0]->duty_name, 'level' => $dutyData[0]->duty_level];
                $data['new_create_time'] = date('Y-m-d', time());
                return $data;
            } else {
                return [];
            }
        } else {
            return $userObj;
        }
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description:  获得新建任务的参与人员
     * @time:2017年7月12日 13:32:30
     * @return false|\PDOStatement|string|\think\Collection
     */

    public static function getPartPerson()
    {
        return Db::name('user')->where(array('status' => 0))->select();
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description: 获取搜索的信息
     * @time: 2017-7-18 09:08:56
     * @param $search
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function getUidBySearch($search)
    {
        return $this->whereLike('realname', '%' . $search . '%')->field('uid,duty_id')->find();
    }
}